package com.starinside.wp.repository;

import com.starinside.wp.domain.UserActiveRegistration;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.Arrays;
import java.util.List;


    public interface UserActiveRegistrationRepository extends JpaRepository<UserActiveRegistration, String>, JpaSpecificationExecutor<UserActiveRegistration> {


    //    Long findCountByAreaIdAndActivityId(String areaId, String s);


        List<UserActiveRegistration> findAllByActivityIdAndAreaId(String activityId, String areaId);
        @Query(value = "SELECT COUNT(uar) FROM UserActiveRegistration uar WHERE uar.areaId = :areaId AND uar.activityId = :activityId AND uar.isActive = true ", nativeQuery = true)
        Long findCountByAreaIdAndActivityId(@Param("areaId") String areaId, @Param("activityId") String activityId);

        @Query("SELECT COUNT(uar) FROM UserActiveRegistration uar WHERE uar.activityId = :activityId AND uar.isActive = true ")
        Long findCountByAreaIdAndActivityId1( @Param("activityId") String activityId);

        @Query(value ="SELECT * FROM user_active_registration uar WHERE uar.activityId = :activityId AND uar.childrenId = :childrenId", nativeQuery = true)
        UserActiveRegistration findByActivityIdAndChildrenId(@Param("activityId") String activityId, @Param("childrenId") String childrenId);

        // 批量查询用户注册信息（根据多个活动ID和儿童ID）
        @Query("SELECT u FROM UserActiveRegistration u " +
                "WHERE u.activityId IN :activityIds " +
                "AND u.childrenId = :childrenId")
        List<UserActiveRegistration> findByActivityIdInAndChildrenId(
                @Param("activityIds") List<String> activityIds,
                @Param("childrenId") String childrenId
        );

        // 批量统计每个活动的参与人数
        @Query("SELECT u.activityId, COUNT(u.id) " +
                "FROM UserActiveRegistration u " +
                "WHERE u.activityId IN :activityIds and u.isActive=true " +
                "GROUP BY u.activityId")
        List<Object[]> countByActivityIds(
                @Param("activityIds") List<String> activityIds
        );

        List<UserActiveRegistration> findAllByActivityId(String activityIds);
//    @Query(value = "SELECT COUNT(*) FROM user_active_registration uar WHERE uar.area_id = :areaId AND uar.activity_id = :activityId AND uar.is_active = :isActive", nativeQuery = true)
//    Long findCountByAreaIdAndActivityIdAndIsActive(@Param("areaId") String areaId, @Param("activityId") String activityId, @Param("isActive") boolean isActive);
}
